package com.gtone.usbtool.encrypt;

import android.net.Uri;
import android.text.TextUtils;
import android.text.format.DateFormat;

import org.json.JSONObject;

import java.io.File;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    private final static int START = 0;
    private final static int STOP = 1;
    private static int STATE = START;

    private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    private final static String UNITS[] = {" bytes", " KB", " MB", " GB",
            " TB"};
    private final static int MODEL = 1024;

    private StringUtil() {
    }

    public static boolean isBoolean() {
        if (STATE == START) {
            STATE = STOP;
            return true;
        } else {
            STATE = START;
            return false;
        }
    }


    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    public static boolean isQQNumber(String mobiles) {
        Pattern p = Pattern.compile("^[1-9][0-9]{4,}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isTelephone(String strEmail) {
        String strPattern = "^(0[0-9]{2,3}\\-)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    /**
     * 判断是否为手机号
     *
     * @return
     */
    public static boolean isPhoneNumber(final String str) {
        if (str == null)
            return false;
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    public static boolean isLongitude(String Longitude) {
        String regExpress = "((^[-]?[0-9])|(^[-]?[2-9]([0-9])?)|(^[-]?[1](([0-7])?)(([0-9])?)))(([.](([0-9])|([1-5][0-9])))?){1,2}$";//[°][′]["]
        return Pattern.matches(regExpress, Longitude);

    }

    public static boolean isLatitude(String Latitude) {
        String regExpress = "((^[-]?[0-9])|(^[-]?[1-8]([0-9])?))(([.](([0-9])|([0-5][0-9])))?){1,2}$";//°′"
        return Pattern.matches(regExpress, Latitude);
    }

    public static boolean isMobilephone(String mobiles) {
        if (mobiles.startsWith("86") || mobiles.startsWith("+86")) {
            mobiles = mobiles.substring(mobiles.indexOf("86") + 2);
        }
        /*Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");*/
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isZipCode(String mobiles) {
        Pattern p = Pattern.compile("[0-9]\\d{5}(?!\\d)");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isNotBlankAndEmpty(String str) {
        if (str != null && str.length() > 0) {
            return true;
        }
        return false;
    }

    public static boolean isIp(String value) {
        if (TextUtils.isEmpty(value)) {
            return false;
        }

        String[] fields = value.split("\\.");
        if (fields.length != 4) {
            return false;
        }

        boolean result = true;
        for (int i = 0; i < fields.length; i++) {
            try {
                int number = Integer.parseInt(fields[i]);
                result &= (number >= 0 && number <= 255);
            } catch (NumberFormatException e) {
                result = false;
            }

            if (!result) {
                break;
            }
        }
        return result;
    }

    public static String toHex(byte[] b) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            int v = b[i];
            builder.append(HEX[(0xF0 & v) >> 4]);
            builder.append(HEX[0x0F & v]);
        }
        return builder.toString();
    }

    public static byte[] hexToBytes(String str) {
        int len = str.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < b.length; i++) {
            int start = i * 2;
            String v = str.substring(start, start + 2);
            try {
                b[i] = (byte) Integer.parseInt(v, 16);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return b;
    }

    public static String strToHourAndMin(int value) {
        String str = "";
        if ((value + "").length() == 1) {
            str = "0" + value;
        } else {
            str = value + "";
        }
        return str;
    }

    public static boolean isEquals(String str1, String str2) {
        boolean result = false;
        if (str1 != null) {
            result = str1.equals(str2);
        } else if (str2 == null) {
            result = true;
        }
        return result;
    }

    public static String toSqliteStr(String str) {
        String result = null;
        if (str != null) {
            result = str.replaceAll("'", "''");
            result = result.replaceAll("%", "/%");
            result = result.replaceAll("_", "/_");
        }
        return result;
    }

    public static String toFileSize(long size) {
        int lastIndex = 0;
        long value = size;
        long last = 0L;

        for (int i = 0; i < UNITS.length; i++) {
            long newValue = value / MODEL;
            if (newValue <= 0) {
                break;
            }

            last = value % MODEL;
            value = newValue;
            lastIndex = i + 1;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(value);
        if (last > 0L) {
            float f = (last + 0.0f) / (MODEL + 0.0f);
            int i = Math.round(f * 100);
            if (i > 0) {
                builder.append(".").append(i);
            }
        }
        builder.append(UNITS[lastIndex]);
        return builder.toString();
    }

    public static String getDateString(long time) {
        return (String) DateFormat.format("yyyy-MM-dd", time * 1000);
    }

    public static int nullSafeStringComparator(final String one,
                                               final String two) {
        if (one == null ^ two == null) {
            return (one == null) ? -1 : 1;
        }

        if (one == null && two == null) {
            return 0;
        }
        return one.compareToIgnoreCase(two);
    }

    public static byte[] toBytes(String str) {
        int len = str.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < b.length; i++) {
            int start = i * 2;
            String v = str.substring(start, start + 2);
            try {
                b[i] = (byte) Integer.parseInt(v, 16);
            } catch (NumberFormatException e) {
                e.getMessage();
                return null;
            }
        }
        return b;
    }

    public static String[] split(String string, String delimiters) {
        StringTokenizer st = new StringTokenizer(string, delimiters);
        String[] fields = new String[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++) {
            String token = st.nextToken();
            fields[i] = token;
        }

        return fields;
    }

    public static int versionCompare(String version1, String version2) {
        if (version1 == null && version2 == null) {
            return 0;
        } else if (version1 == null) {
            return -1;
        } else if (version2 == null) {
            return 1;
        } else {
            String[] fields1 = split(version1, ".");
            String[] fields2 = split(version2, ".");
            int loop = Math.min(fields1.length, fields2.length);

            int result = 0;
            for (int i = 0; i < loop; i++) {
                result = fields1[i].compareTo(fields2[i]);
                if (result != 0) {
                    return result;
                }
            }

            return fields1.length - fields2.length;
        }
    }

    public static String formatGB(String format, long usedSize,
                                  long availableSize) {
        return String.format(format, usedSize / (1024.0 * 1024.0 * 1024.0),
                availableSize / (1024.0 * 1024.0 * 1024.0));
    }

    public static String formatJsonString(String jsonObj) {
        if (!jsonObj.startsWith("{"))
            jsonObj = jsonObj.substring(1);

        return jsonObj;
    }

    public static boolean isNullJsonObj(JSONObject json, String name) {
        return json != null ? json.isNull(name) : false;
    }

    public static String unEncode(String data) {
        if (null == data || "".equals(data))
            return data;
        data = data.replaceAll("&quot;", "\"");
        data = data.replaceAll("&lt;", "<");
        data = data.replaceAll("&gt;", ">");
        data = data.replaceAll("&#92;", "\\\\");
        data = data.replaceAll("&apos;", "'");
        data = data.replaceAll("&amp;", "&");
        return data;
    }

    /**
     * 检查字符串是否为null或者为空
     *
     * @param s
     * @return
     */
    public static boolean emptyOrNull(String s) {
        if (s == null) {
            return true;
        }
        if ("".equals(s)) {
            return true;
        }
        return false;
    }

    /**
     * 将string转为int ，异常时返回-1
     *
     * @param s
     * @return
     */
    public static int toInt(String s) {
        int i = 0;
        try {
            i = Integer.parseInt(s);
        } catch (Exception e) {
            i = -1;
        }
        return i;
    }

    public static final String EMPTY = "";

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isBlank(String str) {
        return (null == str || str.length() <= 0);
    }

    public static String substringAfter(String str, String separator) {
        if (isEmpty(str)) {
            return str;
        }
        if (separator == null) {
            return EMPTY;
        }
        int pos = str.indexOf(separator);
        if (pos == -1) {
            return EMPTY;
        }
        return str.substring(pos + separator.length());
    }

    public static String substringBefore(String str, String separator) {
        if (isEmpty(str) || separator == null) {
            return str;
        }
        if (separator.length() == 0) {
            return EMPTY;
        }
        int pos = str.indexOf(separator);
        if (pos == -1) {
            return str;
        }
        return str.substring(0, pos);
    }

    /**
     * 整数转时分
     *
     * @param time
     * @return
     */
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":"
                        + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static boolean isGif(String imagePath) {
        if (getExtensionName(imagePath).equals("gif")) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Java文件操作 获取文件扩展名
     * */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    public static boolean isLegalId(String id) {
        if (id.toUpperCase().matches("(^\\d{15}$)|(^\\d{17}([0-9]|X)$)")) {
            return true;
        } else {
            return false;
        }
    }

    public static String encodeUrl(String url) {
        return Uri.encode(url, "-![.:/,%?&=]");
    }

    public static boolean isWebUrl(String webUrl) {
        String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
        Pattern httpPattern = Pattern.compile(regex);
        Matcher m = httpPattern.matcher(webUrl);
        return m.matches();
    }

//    public static boolean isTelephonyNumber(String phoneNo) {
//        if (TextUtils.isEmpty(phoneNo)) {
//            return false;
//        }
//        if (phoneNo.length() == 11) {
//            for (int i = 0; i < 11; i++) {
//                if (!android.telephony.PhoneNumberUtils.isISODigit(phoneNo.charAt(i))) {
//                    return false;
//                }
//            }
//            Pattern p = Pattern.compile("^((13[^4,\\D])" +
//                    "|(17[0-9])" +
//                    "|(19[0-9])" +
//                    "|(134[^9,\\D])" +
//                    "|(14[5,7])" +
//                    "|(15[^4,\\D])" +
//                    "|(17[3,6-8])" +
//                    "|(18[0-9]))\\d{8}$");
//            Matcher m = p.matcher(phoneNo);
//            return m.matches();
//        }
//        return false;
//    }

    public static boolean isMobilPhone(String phone) {
        if (StringUtil.isEmpty(phone)) {
            return false;
        }
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9])|(19[0-9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    /**
     * 正则表达式：验证护照
     */
    public static final String REGEX_ID_PASSPORT = " /^1[45][0-9]{7}|([P|p|S|s]\\d{7})|([S|s|G|g]\\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\\d{8})|([H|h|M|m]\\d{8,10})$/";
    /**
     * 正则表达式：验证军官证
     */
    public static final String REGEX_ID_COO = " /[\\u4e00-\\u9fa5](字第){1}(\\d{4,8})(号?)$/";

    public static boolean isOfficerCard(String card) {
        Pattern p = Pattern.compile(REGEX_ID_COO);
        Matcher m = p.matcher(card);
        return !m.matches();

    }

    //正则 去herf
    public static final String REGEX_Herf = ".*href=\"(.*?)\"";

//    let reg3 = /(?<=>)(.*?)(?=<\/a>)/  // 获取指定“>”之后  “</a>”之前的内容
//            str.match(reg3) // 5456456度

//    String url = regex.getValue(temp, ".*href=\"(.*?)\"");
    public static String url(String url) {
//        Pattern pattern_a = Pattern.compile("<a[^>]*href=(\\\"([^\\\"]*)\\\"|\\'([^\\']*)\\'|([^\\\\s>]*))[^>]*>(.*?)</a>");
//        Pattern pattern_a = Pattern.compile("(<a.+?)(href=\"([^\"]*?)\")[\\s>](.*)");
//        Pattern pattern_a = Pattern.compile(("<a[^>]+href=[\"\\'](.*?)[\"\\']"));
        Pattern pattern_a = Pattern.compile("<a.*?href=[\"']?((https?://)?/?[^\"']+)[\"']?.*?>(.+)</a>");
        Matcher matcher_a = pattern_a.matcher(url);
        if(matcher_a.find()){
            return matcher_a.group(1);
        }else {
            return  null;
        }



    }



    public static boolean isPassPortCard(String card) {
        Pattern p = Pattern.compile(REGEX_ID_PASSPORT);
        Matcher m = p.matcher(card);
        return !m.matches();
    }


    public static boolean isNumber(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean validateIdNumber(String idNumber) {
        Pattern p1 = Pattern.compile("(\\d{17}[0-9xX]|\\d{14}[0-9xX])");
        Matcher matcher = p1.matcher(idNumber);
        if (matcher.matches()) {
            Pattern p2 = Pattern.compile("\\d{6}(\\d{8}).*"); // 用于提取出生日字符串
            Pattern p3 = Pattern.compile("(\\d{4})(\\d{2})(\\d{2})");// 用于将生日字符串进行分解为年月日
            Matcher matcher2 = p2.matcher(idNumber);
            boolean flag = matcher2.find();
            if (flag) {
                String date = matcher2.group(1);
                Matcher matcher3 = p3.matcher(date);
                if (matcher3.find()) {
                    String year = matcher3.group(1);
                    int month = Integer.parseInt(matcher3.group(2));
                    int day = Integer.parseInt(matcher3.group(3));
                    if (month < 1 || month > 12) {
                        return false;
                    }
                    if (day > getDays(year, month) || day < 1) {
                        return false;
                    }
                    return true;
                }
            }
        }
        return false;
    }

    public static int getDays(String year, int month) {
        int days = 0;
        switch (month) {
            case 1:
                days = 31;
                break;
            case 3:
                days = 31;
                break;
            case 5:
                days = 31;
                break;
            case 7:
                days = 31;
                break;
            case 8:
                days = 31;
                break;
            case 10:
                days = 31;
                break;
            case 12:
                days = 31;
                break;
            case 2:
                if (isLeapYear(year))
                    days = 29;
                else
                    days = 28;
                break;
            default:
                days = 30;
        }
        return days;
    }

    public static boolean isLeapYear(String year) {
        Long yearL = Long.parseLong(year);
        if ((yearL % 4 == 0) && (yearL % 100 != 0) || (yearL % 400 == 0)) {
            return true;
        }
        return false;
    }

    public static boolean isPassport(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^1[45][0-9]{7}|([P|p|S|s]\\d{7})|([S|s|G|g]\\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\\d{8})|([H|h|M|m]\\d{8,10})$");
        Matcher isPassport = pattern.matcher(str);
        if (!isPassport.matches()) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isOfficer(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[a-zA-Z0-9]{7,21}$");
        Matcher isOfficer = pattern.matcher(str);
        if (!isOfficer.matches()) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean isHttpUrl(String urls) {
        //http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
        String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";//设置正则表达式
        Pattern pat = Pattern.compile(regex.trim());//对比
        Matcher mat = pat.matcher(urls.trim());
        if (mat.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean deleteDirWithFile(File fileRoot) {
        if (fileRoot.isDirectory()) {
            for (File file : fileRoot.listFiles()) {
                if (file.isFile()) {
                    file.delete();
                } else if (file.isDirectory()) {
                    deleteDirWithFile(file);
                }
            }
            return fileRoot.delete();// 删除目录本身
        } else if (fileRoot.isFile()) {
            return fileRoot.delete();
        }
        return false;
    }

    /**
     *      * 获取文件大小
     *      * 
     *      * @param size
     *      * @return
     *     
     */
    public static String getPrintSize(long size) {
        // 如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        // 如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        // 因为还没有到达要使用另一个单位的时候
        // 接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            // 因为如果以MB为单位的话，要保留最后1位小数，
            // 因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "MB";
        } else {
            // 否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "GB";
        }
    }

    public static boolean isStartWith(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[a-zA-Z]\\w{5,15}$");
        Matcher isOfficer = pattern.matcher(str);
        if (!isOfficer.matches()) {
            return false;
        } else {
            return true;
        }
    }

}
